<?php
define('APPTYPEID', 4);
define('CURSCRIPT', 'portal');

require './source/class/class_core.php';

function discuz_publish($title, $content, $fid)
{

	$discuz = & discuz_core::instance();
	$discuz->init();

	$title = $title;//mysql_escape_string($title);              //标题                                      //版块
	$uid = 4;                                        //发帖人ID
	$uname='wiwi';                                //发帖人
	$time=time();                                  //发帖时间 
	$content = $content;//mysql_escape_string($content);    //内容
	$ip='127.0.0.1';                               //IP
	$views=rand(0,100);                            //浏览次数
	$htmlon=1;                                     // 是否支持HTML   1是支持. 要去后台给板块开启支持HTML

	$result = DB::fetch(DB::query('SELECT fid FROM ' . DB::table('forum_thread') . " WHERE subject='" . $title . "'"));
	if(!empty($result['fid'])) return;

	
	$array=array('fid'=>$fid,'posttableid'=>0,'typeid'=>0,'sortid'=>0,'readperm'=>0,'price'=>0,'author'=>$uname,'authorid'=>$uid,'subject'=>$title,'dateline'=>$time,'lastpost'=>$time,'lastposter'=>$uname,'views'=>$views,'replies'=>0,'displayorder'=>0,'highlight'=>0,'digest'=>0,'rate'=>0,'special'=>0,'attachment'=>0,'moderated'=>0,'closed'=>0,'stickreply'=>0,'recommends'=>0,'recommend_add'=>0,'recommend_sub'=>0,'heats'=>0,'status'=>32,'isgroup'=>0,'favtimes'=>0,'sharetimes'=>0,'stamp'=>-1,'icon'=>-1,'pushedaid'=>0,'cover'=>0,'replycredit'=>0);
	
	$tid=DB::insert('forum_thread', $array, TRUE);

	$array=array('fid'=>$fid,'tid'=>$tid,'first'=>1,'author'=>$uname,'authorid'=>$uid,'subject'=>$title,'dateline'=>$time,'message'=>"{$content}",'useip'=>$ip,'invisible'=>0,'anonymous'=>0,'usesig'=>1,'htmlon'=>0,'bbcodeoff'=>0,'smileyoff'=>-1,'parseurloff'=>0,'attachment'=>0,'rate'=>0,'ratetimes'=>0,'status'=>0,'tags'=>'','comment'=>0,'replycredit'=>0);

	
	$pid = DB::fetch(DB::query('SELECT MAX(pid) AS pid FROM ' . DB::table('forum_post')));
	$array['pid'] = $pid['pid'] + 1;
	DB::insert('forum_post', $array, TRUE);

	$array = array('pid'=>$pid['pid']+1);
	DB::insert('forum_post_tableid', array('pid'=>''), true);



	$num=DB::fetch(DB::query('SELECT threads,posts,todayposts FROM '.DB::table('forum_forum')." WHERE fid='$fid'"));
	$array=array('threads'=>$num['threads']+1,'posts'=>$num['posts']+1,'lastpost'=>'$time','todayposts'=>$num['todayposts']+1);
	DB::update('forum_forum', $array,"fid='$fid'");



	DB::query("UPDATE ".DB::table('common_member_count')." SET posts=posts+1 WHERE uid='$uid'", 'UNBUFFERED');
	DB::query("UPDATE ".DB::table('common_member_status')." SET lastip='$ip',lastvisit='$time',lastactivity='$time',lastpost='$time' WHERE uid='$uid'", 'UNBUFFERED');

	echo 'OK'.$pid . "<>";
}

function discuz_clear($str)
{
	$discuz = & discuz_core::instance();
	$discuz->init();
	
	$ret = DB::fetch_all('SELECT tid, message FROM ' . DB::table('forum_post'));
//var_dump($ret);
	foreach($ret as $result)
	{
		if(($pos = stripos($result['message'], $str)) !== false)
		{
			$content = substr($result['message'], $pos + strlen($str));
			$tid = $result['tid'];
			DB::update('forum_post', array('message'=>$content), "tid='{$tid}'");
		}
	}
}

if($_POST['title'])
{
	$title = trim($_POST['title']);
	$message = trim($_POST['message']);
	$fid = trim($_POST['fid']);

	if(empty($title) || empty($message) || empty($fid))
	{
		echo ' some field empty!';
		exit(0);
	}

	discuz_publish($title, $message, $fid);
	echo 1;

}



?>